package trombinoscope.servlet;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the edior.
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import trombinoscope.modele.Database;
import javax.servlet.ServletContext;
import trombinoscope.modele.Utilisateur;
import trombinoscope.modele.UtilisateurDAO;
/**
 *
 * @author jef
 */

@WebServlet(name = "ServletAjouter", urlPatterns = {"/ServletAjouter"})
public class ServletAjouter extends HttpServlet {
    
  
    
    protected void processRequest (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
          //String idUser = request.getParameter("idUser");
            String nom = request.getParameter("nom");
            String prenom = request.getParameter("prenom");
            String date_naissance = request.getParameter("date_naissance");
            String adresse = request.getParameter("adresse");
            String telephone_fixe = request.getParameter("num_fixe");
            String telephone_portable = request.getParameter("num_portable");
            String email = request.getParameter("email");
            String password = request.getParameter("password");
            String droit_acces = request.getParameter("droit_acces");
            
            
            
            int droitAcces = Integer.parseInt(droit_acces);
            Boolean b = Boolean.getBoolean(droit_acces);
            
            String idGroupe = request.getParameter("id_groupe");
            int idGroupeInt = Integer.parseInt(idGroupe);
           // String photo = request.getParameter("chemin_photo");
            String description = request.getParameter("description");
            //String id_groupe = request.getParameter("id_groupe");
            //int idGroupe = Integer.parseInt(id_groupe);
            
            String photo= null;
          
            Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
       // Utilisateur u = UtilisateurDAO.getUserByEmail(email);
        
        try 
        {
            
                          
                Class.forName("com.mysql.jdbc.Driver"); 
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/trombinoscope","root","");
            String sql = "INSERT INTO utilisateur(idUser, nomUtilisateur, prenom, date_naissance, adresse, numTelDomicile, numTelPortable, email, motDePasse) VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?)";
            ps = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            
            ps.setString(1, nom);
            ps.setString(2, prenom);
            ps.setString(3, date_naissance);
            ps.setString(4, adresse);
            ps.setString(5, telephone_fixe);
            ps.setString(6, telephone_portable);
            ps.setString(7, email);
            ps.setString(8, password);
            
            ps.executeUpdate(); 
           
            rs = ps.getGeneratedKeys();  // id de l'utilisateur qui a été attribué
            rs.next();
      
            int idUser = rs.getInt(1);
            System.out.println("Nouvel id de l'utilisateur : " + idUser);
      
            String sql2 = "INSERT INTO etudiant(idUser, droitAcces, photo, description, idGroupe) VALUES(?, ?, ?, ?, ?)";
            
            
            photo = nom.toUpperCase() +"_"+ prenom+".jpg";
            ps = con.prepareStatement(sql2);
            ps.setInt(1, idUser);
            ps.setInt(2, droitAcces);
            ps.setString(3, photo); //stockage de la photo
            ps.setString(4, description);
            ps.setInt(5, idGroupeInt);
            
            ps.executeUpdate();
            
           
             
                out.println("<script type='text/javascript'> alert('Etudiant ajouté avec succes.') </script>");
                out.println("<script type='text/javascript'> document.location.href='./accueil.jsp' </script>");
            
        }
    
        finally 
        {
           
            Database.close(rs);
            Database.close(ps);
            Database.close(con);
        }   
    }
    
    
            
     
    
     @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {       
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServletAjouter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ServletAjouter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {        
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServletAjouter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ServletAjouter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}   